<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <cost-plan v-model="model.productCodes" v-bind="modalProps" rowKey="code" />
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { postFormAction, postAction, putAction } from '@/api/manage'
import costPlan from '@/views/logistics/sales/modules/costPlan.vue'
export default {
  name: "SysBatchAddForm",
  components: {
    costPlan,
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data () {
    return {
      model:{
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {},
      url: {
        add: "/sys/sysDepart/add",
        edit: "/sys/sysDepart/edit",
        queryById: "/sys/sysDepart/queryById",
        assignProduct: "/logistics/product/logisticsProduct/assignProduct",
      },
      modalProps: {
        modalPropsCustom: true,
        columns: [
          {
            align: "center",
            dataIndex: "name",
            title: "物流产品名称",
            width: "33%",
            isShow: true
          },
          {
            align: "center",
            dataIndex: "channelName",
            title: "渠道名称",
            width: "33%",
            isShow: true
          },
          {
            align: "center",
            dataIndex: "costPriceId_dictText",
            title: "成本价方案",
            width: "33%",
            isShow: true
          }
        ],
        listUrl: "/logistics/product/logisticsProduct/list",
        multiple: true,
        queryParamConfig: {
          ableStatus: '1,2,3'
        },
        queryConfig: [
          {key: 'name', label: '物流产品'},
          {key: 'channelId', label: '渠道名称', dictCode: "hz_logistics_channel,channel_name,id,logistics,is_enable='Y'"},
          {key: 'costPriceId', label: '成本价方案', dictCode: "hz_logistics_cost_price,name,id,logistics,able_status='2'"},
        ]
      },
      selectedRowKeys_: [],
    }
  },
  computed: {
    formDisabled(){
      return this.disabled
    },
  },
  created () {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    add () {
      this.edit(this.modelDefault);
    },
    edit (record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm () {
      const that = this;
      // 触发表单验证
      that.$refs.form.validate(valid => {
        if (valid) {
          let httpurl = '';
          let Action;
          let params = Object.assign({}, that.model);
          params["orgCategory"] ="1";  //默认为公司创建
          if(!that.model.id){
            Action = postFormAction;
            httpurl+=that.url.assignProduct;
            if(!that.model.productCodes){
              that.$message.warning('请选择物流产品');
              return;
            }
            params = {
              productCodes: that.model.productCodes,
              ids: that.selectedRowKeys_.join(',')
            }
          }else{
            Action = putAction;
            httpurl+=that.url.edit;
          }
          that.confirmLoading = true;
          Action(httpurl,params).then((res)=>{
            if(res.success){
              that.$message.success(res.message);
              that.$emit('ok');
            }else{
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }
      })
    }
  }
}
</script>